package com.fanyouzhao.joker.service;

import net.sf.json.JSONObject;
import okhttp3.*;

/**
 * author: fanyouzhao
 * date: 22:35 2021/9/5
 */
public class Login {
    public static String login(String username, String password) {
        try {
            OkHttpClient client = new OkHttpClient();
            RequestBody body = new FormBody.Builder()
                    .add("username", username)
                    .add("password", password)
                    .build();
            Request request = new Request.Builder()
                    .url("https://vip.jokerps.com/wp-json/jwt-auth/v1/token")
                    .addHeader("content-type", "application/x-www-form-urlencoded")
                    .post(body)
                    .build();
            Response response = client.newCall(request).execute();
            String responseData = null;
            if (response.body() != null) {
                responseData = response.body().string();
            }
            System.out.println("------------------------登录响应------------------------\n"
                    + responseData);
            JSONObject jsonObject = JSONObject.fromObject(responseData);
            return jsonObject.getString("token");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
